package com.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.Date;
import java.text.SimpleDateFormat;

@Service
public class TimeService {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    public void startTimePrintingTask() {
        threadPoolExecutor.execute(() -> {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (int i = 0; i < 60; i++) {
                try {
                    System.out.println("当前时间: " + dateFormat.format(new Date()));
                    Thread.sleep(1000); // 每秒执行一次
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
            System.out.println("任务执行完成");
        });
    }
    
    // 倒计时任务
    public void startCountdownTask() {
        threadPoolExecutor.execute(() -> {
            for (int i = 60; i > 0; i--) {
                try {
                    System.out.println("倒计时: " + i + " 秒");
                    Thread.sleep(1000); // 每秒执行一次
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
            System.out.println("倒计时结束");
        });
    }
}